<script lang="ts" setup>
import { Snackbar } from "@varlet/ui";
import mediumZoom from "medium-zoom";
const herfMap = {
  QQ: "https://ferret.icu/file/qq.webp",
  Github: "https://github.com/FerretAngel",
  哔哩哔哩: "https://space.bilibili.com/122548442",
  微信: "https://ferret.icu/file/wechat.webp",
};
function herf(key: keyof typeof herfMap) {
  // 如果是手机
  if (
    key === "QQ" &&
    /(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent)
  ) {
    window.open(
      "https://qm.qq.com/cgi-bin/qm/qr?k=f1Cr2Gdys3r7ny0ar9pwSraDleqaX3e0"
    );
    return;
  }
  if (key === "Github" || key === "哔哩哔哩") {
    window.open(herfMap[key]);
    return;
  }
  if (Object.prototype.hasOwnProperty.call(herfMap, key)) {
    const dom = document.querySelector(`[title="${key}"]`);
    const url = herfMap[key];
    const image = new Image();
    image.src = url;
    image.onload = () => {
      if (!dom) {
        window.open(herfMap[key]);
        return;
      }
      const rect = dom.getBoundingClientRect();
      image.style.position = "fixed";
      image.style.top = `${rect.top}px`;
      image.style.left = `${rect.left}px`;
      image.style.width = `${rect.width}px`;
      document.body.appendChild(image);
      const zoom = mediumZoom(image, {
        background: "rgba(0, 0, 0, 0.45)",
      });
      zoom.open();
      zoom.on("close", () => {
        document.body.removeChild(image);
      });
    };
    image.onerror = () => {
      Snackbar.error("加载失败！");
    };
  }
}
</script>

<template>
  <div class="userBody">
    <img
      class="avatar"
      src="https://ferret.icu/api/public/avatar/1219460281"
      alt="avatar"
    />
    <div class="info">
      <h3>FERRET</h3>
      <p>Web前端实习生</p>
    </div>
    <div class="link">
      <Icon
        @click="herf('QQ')"
        icon="ant-design:qq-circle-filled"
        title="QQ"
      ></Icon>
      <Icon
        @click="herf('微信')"
        icon="ant-design:wechat-filled"
        title="微信"
      ></Icon>
      <Icon
        @click="herf('Github')"
        icon="ant-design:github-filled"
        title="Github"
      ></Icon>
      <Icon
        @click="herf('哔哩哔哩')"
        icon="ri:bilibili-fill"
        title="哔哩哔哩"
      ></Icon>
    </div>
  </div>
</template>

<style lang="less" scoped>
.userBody {
  padding: 1em 3em;
  display: flex;
  flex-direction: column;
  align-items: center;
  border-radius: 0.5em;
  color: var(--color-primary);
  justify-content: space-evenly;
  box-shadow: 0.1em 0.1em 1em #ccc;

  .link {
    margin-top: 1em;
    display: flex;
    align-items: center;
    justify-content: space-between;
    // width: max(50%, 10em);
    width: 100%;
    svg {
      cursor: pointer;
      transition: all 0.3s;
      border-radius: 50%;
      padding: 0.1em;
      font-size: 2em;
      color: var(--color-body);
      background-color: var(--color-primary);
      &:hover {
        background-color: var(--color-info);
      }
    }
  }
  .avatar {
    clip-path: circle(50% at 50% 50%);
    width: 5em;
    box-shadow: 0 0 0.5em rgba(0, 0, 0, 0.5);
    aspect-ratio: 1/1;
  }
  .info {
    margin-top: 0.5em;
    text-align: center;

    h3 {
      margin-bottom: 0.2em;
      font-size: 2em;
      letter-spacing: 0.1em;
      font-weight: 500;
    }
    p {
      font-size: 0.8em;
      opacity: 0.8;
      color: var(--color-text);
    }
  }
}
</style>
